package com.example.tests;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;

public class TestBase {

	protected ApplicationManager app;
	Random rnd = new Random();
	
	public int getRandomNumberBetween(int start, int end) {
	return new Random().nextInt(end - start) + start;
	}
	
	@BeforeClass
	public void setUp() throws Exception {
		app = ApplicationManager.getInstance();
	}
	
	@AfterTest
	public void tearDown() throws Exception {
		ApplicationManager.getInstance().stop();
	}

	@DataProvider(name = "randomContacts")
	public Iterator<Object[]> generateRandomContacts() {
		//Set<GroupObject> groupsCache = app.getGroupHelper().getGroups();
		//Iterator<GroupObject> someGroup = groupsCache.iterator();
		//someGroup.getGroupName() {
	
		String[] MONTHS = new String[]{"January", "February", "March",
				"April", "May", "June", "July", "August",
				"September", "October", "November", "December"};
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++) {
			int year = getRandomNumberBetween(1900, 2012);
			int day = getRandomNumberBetween(1, 31);
			//Count contacts
			
			Set<ContactObject> contactsList = app.getContactHelper().getContacts();
			int lastContact =  contactsList.size();
			int contactNumber = getRandomNumberBetween(1, lastContact);
			String month = MONTHS[new Random().nextInt(12)];
			ContactObject contact = new ContactObject()
			.setId(null)
			.setFirstName("FirstName" + rnd.nextInt())
			.setLastName("LastName" + rnd.nextInt())
			.setMainAddress("oak str" + rnd.nextInt())
			.setHomeTel("0" + rnd.nextInt())
			.setMobileTel("8" + rnd.nextInt())
			.setWorkTel("800" + rnd.nextInt())
			.setMainMail("mail" + rnd.nextInt() + "@mail.com")
			.setOtherMail("othermail" + rnd.nextInt() + "@yahoo.com")
			.setNewGroup(null)
			.setBirthDay(String.valueOf(day))
			.setBirthMonth(month)
			.setBirthYear(String.valueOf(year))
			.setOtherAddress("none")
			.setOtherPhone("none");
			list.add(new Object[]{contact, contactNumber});
		}
		return list.iterator();
	}

	@DataProvider(name = "randomGroups")
	public Iterator<Object[]> generateRandomGroups() {
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++) {
		GroupObject group = new GroupObject()
			.setName("name"+rnd.nextInt())
			.setHeader("header"+rnd.nextInt())
			.setFooter("name"+rnd.nextInt());
		list.add(new Object[]{group});
		//list.add(new Object[]{group, "string"});
		}
		return list.iterator();
	}
}
